using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using Model.Database;

namespace Model.Mapping
{
    public class PowerMap : EntityTypeConfiguration<Power>
    {
        public PowerMap()
        {
            // Primary Key
            this.HasKey(t => t.PowerId);

            // Properties
            this.Property(t => t.PowerId)
                .IsRequired()
                .HasMaxLength(32);

            this.Property(t => t.ParentId)
                .IsRequired()
                .HasMaxLength(32);

            this.Property(t => t.PowerName)
                .IsRequired()
                .HasMaxLength(25);

            this.Property(t => t.Url)
                .HasMaxLength(75);

            this.Property(t => t.MenuIcon)
                .HasMaxLength(100);

            this.Property(t => t.ControllerName)
                .HasMaxLength(25);

            this.Property(t => t.ActionName)
                .HasMaxLength(25);

            this.Property(t => t.Description)
                .HasMaxLength(50);

            // Table & Column Mappings
            this.ToTable("Power");
            this.Property(t => t.PowerId).HasColumnName("PowerId");
            this.Property(t => t.ParentId).HasColumnName("ParentId");
            this.Property(t => t.PowerName).HasColumnName("PowerName");
            this.Property(t => t.Url).HasColumnName("Url");
            this.Property(t => t.IsPost).HasColumnName("IsPost");
            this.Property(t => t.IsMenu).HasColumnName("IsMenu");
            this.Property(t => t.MenuIcon).HasColumnName("MenuIcon");
            this.Property(t => t.ControllerName).HasColumnName("ControllerName");
            this.Property(t => t.ActionName).HasColumnName("ActionName");
            this.Property(t => t.Description).HasColumnName("Description");
            this.Property(t => t.Sort).HasColumnName("Sort");
            this.Property(t => t.AddTime).HasColumnName("AddTime");
            this.Property(t => t.ModifyTime).HasColumnName("ModifyTime");

            // Relationships
            this.HasMany(t => t.Roles)
                .WithMany(t => t.Powers)
                .Map(m =>
                    {
                        m.ToTable("R_Role_Power");
                        m.MapLeftKey("Power_PowerId");
                        m.MapRightKey("Role_RoleId");
                    });


        }
    }
}
